/**
 * date：2010-11-15
 * author：eason
 * description：按时间间隔顺序执行序列动作的动作类.
 * 该类会每间隔一个设置的时间,执行动作序列中的一个动作,而不管之前的动作是否已经执行完毕.
 */
package com.pickgliss.action
{
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	public class TickOrderQueueAction extends OrderQueueAction
	{
		private var _interval : uint;
		private var _tickTimer : Timer;
		
		private var _delay : uint;		
		private var _delayTimer : Timer;
		
		/**
		 * 构造函数. 
		 * @param actList 需执行的动作数组,成员需实现 IAction 接口.
		 * @param interval 动作之间的间隔执行时间.
		 * @param delay 延迟执行的毫秒数,为0时表示不延时,直接执行.
		 * @param timeOut 超时时间.为0时表示不限时.
		 * 
		 */	
		public function TickOrderQueueAction(actList:Array, interval : uint = 100, delay : uint = 0, timeOut:uint=0)
		{
			_interval = interval;
			_delay = delay;
			super(actList, timeOut);
		}
		override public function act():void
		{
			if(_delay > 0){
				_delayTimer = new Timer(_delay, 1);
				_delayTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onDelayTimerComplete);
				_delayTimer.start();
			}else{
				super.act();
			}
		}
		private function onDelayTimerComplete(event : TimerEvent):void
		{
			removeDelayTimer();
			
			super.act();
		}
		private function removeDelayTimer():void
		{
			if(_delayTimer){
				_delayTimer.stop();
				_delayTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, onDelayTimerComplete);
				_delayTimer = null;
			}
			
		}
		override protected function actOne():void
		{
			var action : IAction = _actList[_count] as IAction;
			
			//action.setCompleteFun(actOneComplete);//该行属于父类, 注释此行并换成定时执行下一个动作.
			startTickTimer();
			
			action.act();
		}
		private function startTickTimer():void
		{
			_tickTimer = new Timer(_interval, 1);
			_tickTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onTickTimerComplete);
			_tickTimer.start();
		}

		private function onTickTimerComplete(event:TimerEvent):void
		{
			removeTickTimer();
			actNext();
		}
		private function removeTickTimer():void
		{
			if(_tickTimer){
				_tickTimer.stop();
				_tickTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, onTickTimerComplete);
				_tickTimer = null;
			}
		}
		override protected function onLimitTimerComplete(event:TimerEvent):void
		{
			removeTickTimer();
			super.onLimitTimerComplete(event);
		}
		override public function cancel():void
		{
			removeTickTimer();
			super.cancel();
		}
	}
}